package com.dozen.commonbase.view.autopull;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.dozen.commonbase.R;

import java.lang.ref.WeakReference;

/**
 * @author: Dozen
 * @description:
 * @time: 2021/3/29
 */
public class AutoPollRecyclerView extends RecyclerView {


    private AutoPollTask autoPollTask;
    private boolean running; //表示是否正在自动轮询
    private boolean canRun;//表示是否可以自动轮询

    private boolean canTouch = false; //是否可以触摸
    private int runSpeed = 2; //自动循环速度
    private int autoPollTime = 16; //自动循环时间间隔

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = attrs == null ? null : context.obtainStyledAttributes(attrs, R.styleable.AutoPollRecyclerView);
        if (ta != null) {
            canTouch = ta.getBoolean(R.styleable.AutoPollRecyclerView_canTouch, canTouch);
            runSpeed = ta.getInteger(R.styleable.AutoPollRecyclerView_runSpeed, runSpeed);
            autoPollTime = ta.getInteger(R.styleable.AutoPollRecyclerView_autoPollTime, autoPollTime);
            ta.recycle();
        }

        autoPollTask = new AutoPollTask(runSpeed, autoPollTime, this);
    }

    static class AutoPollTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> mReference;
        private int APRunSpeed = 2;
        private int APRunTime = 16;

        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollTask(int APRunSpeed, int APRunTime, AutoPollRecyclerView reference) {
            this.APRunSpeed = APRunSpeed;
            this.APRunTime = APRunTime;
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }

        @Override
        public void run() {
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
                recyclerView.scrollBy(APRunSpeed, APRunSpeed);
                recyclerView.postDelayed(recyclerView.autoPollTask, APRunTime);
            }
        }
    }

    //开启:如果正在运行,先停止->再开启
    public void start() {
        if (running)
            stop();
        canRun = true;
        running = true;
        postDelayed(autoPollTask, autoPollTime);
    }

    public void stop() {
        running = false;
        removeCallbacks(autoPollTask);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if (!canTouch) {
            return false;
        }
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (running)
                    stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (canRun)
                    start();
                break;
        }
        return super.onTouchEvent(e);
    }
}